﻿//### Check max length
function SetMaxLength(txt,maxLen){
    if (txt.value.length > (maxLen-1)){
        alert("This field is limited to " + maxLen + " characters.\n" + "You have reached the limit.")
        txt.focus()
        txt.value = txt.value.substring(0, maxLen)
    }
} 

/************************************************************************************************************/
/************************************************************************************************************/

//### Email validator
function emailValidator(elem){
    var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
    if(elem.value.length > 0){
        if(elem.value.match(emailExp)){
            return true;
        }else{
            alert('Not a valid email address');
            elem.focus();
            return false;
        }
    }
}  

/************************************************************************************************************/
/************************************************************************************************************/

//### Field Limitor
function textCounter(field,counter,maxlimit,linecounter){

	//### Text width
	var fieldWidth =  parseInt(field.offsetWidth);
	var charcnt = field.value.length;        

	//### Ttrim the extra text
	if (charcnt > maxlimit) { 
		field.value = field.value.substring(0, maxlimit);
	}
	else 
	{ 
	    //### Progress bar percentage
	    var percentage = parseInt(100 - (( maxlimit - charcnt) * 100)/maxlimit) ;
	    document.getElementById(counter).style.width =  parseInt((fieldWidth*percentage)/100)+"px";
	    document.getElementById(counter).innerHTML="Limit: "+percentage+"%"
	    
	    //### Color correction on style from CCFFF -> CC0000
	    setcolor(document.getElementById(counter),percentage,"background-color");
	}
}

function setcolor(obj,percentage,prop){
	obj.style[prop] = "rgb(80%,"+(100-percentage)+"%,"+(100-percentage)+"%)";
}


//### Ensure value is a number
function jsIsNumeric(myVal) {
    if (isNaN(myVal.value)) {
        alert("This field should be a numeric value only.")
        myVal.focus();
    }
}
